/***************************************************************
 *
 * (c) 2008 Markus Dittrich 
 *
 * This program is free software; you can redistribute it 
 * and/or modify it under the terms of the GNU General Public 
 * License Version 3 as published by the Free Software Foundation. 
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License Version 3 for more details.
 *
 * You should have received a copy of the GNU General Public 
 * License along with this program; if not, write to the Free 
 * Software Foundation, Inc., 59 Temple Place - Suite 330, 
 * Boston, MA 02111-1307, USA.
 *
 ****************************************************************/

#ifndef CANVASMINMAXMANAGER_H
#define CANVASMINMAXMANAGER_H

/* STL includes */
#include <vector>

/* Qt includes */
#include <QString>
#include <QObject>

/* boost includes */
#include <boost/tuple/tuple.hpp>
#include <boost/utility.hpp>

/* local includes */
#include "data_object.h"

/* forward declarations */
class QRect;
class GraphCanvas;


/*****************************************************************
 * convenience typedefs
 *****************************************************************/
typedef boost::tuple<double,double> double_tuple;


/*****************************************************************
 *
 * CanvasLayoutManager computes the global data min/max values
 * based on the currently active (i.e. not hidden) data
 *
 *****************************************************************/
class CanvasLayoutManager 
  : 
    public QObject,
    public boost::noncopyable
{

  Q_OBJECT


public:

  explicit CanvasLayoutManager();

  void initialize(const GraphCanvas* parent);
  void update();
  void set_dirty();

  double xmin() const; 
  double xmax() const; 
  double ymin() const; 
  double ymax() const; 
  double xscaling() const;
  double yscaling() const;


public slots:

  void zoom(int amount);
  void pan_x(int amount);
  void pan_y(int amount);
  void focus_region(double xmin, double xmax, double ymin, 
      double ymax);
  void reset();


private:

  /* no copying or assignment */
  CanvasLayoutManager(const CanvasLayoutManager&);
  const CanvasLayoutManager& operator=(const CanvasLayoutManager&);

  /* member functions */
  void compute_global_x_minmax_();
  void compute_global_y_minmax_();
  void compute_x_scaling_();
  void compute_y_scaling_();
  void update_current_minmax_();

  bool is_hidden_(const QString& dataName) const;
  const DataObjectPtr retrieve_data_(const QString& dataName) const;

  /* keep track of the current zoom level/zoom speeds and panning
   * distances */
  int zoom_;
  double xZoomSpeed_;
  double yZoomSpeed_;
  int xPan_;
  double xPanFraction_;     
  int yPan_;
  double yPanFraction_;

  /* keep track if we need to recompute ourselves */
  bool dirty_;

  /* global data min/max */
  double xGlobalMax_;
  double xGlobalMin_;
  double yGlobalMax_;
  double yGlobalMin_;

  /* current data min/max based on zoom level */
  double xCurrentMax_;
  double xCurrentMin_;
  double yCurrentMax_;
  double yCurrentMin_;

  /* global scaling */
  double xScaling_;
  double yScaling_;

  /* data members */
  const GraphCanvas* parent_;
};

#endif
